<template>
  <div>
    <div v-if="netdiskList.length" class="container-box">
      <h3 class="title">現有網絡硬盤</h3>
      <el-descriptions class="margin-top" title="" :column="4" size="medium" border v-for="(item, index) in netdiskList" :key="index">
        <el-descriptions-item>
          <template slot="label">
            網盤名稱：
          </template>
          {{ item.dishName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            網盤IP：
          </template>
          {{ item.dishLocation }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            使用狀態：
          </template>
          <span :class="[ item.state === 0 ? 'green' : 'red']">{{ item.state === 0 ? '未使用' : '使用中' }}</span>
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            操作：
          </template>
          <el-button type="danger" @click="onDelFacility(item.dishId)" size="small">刪除</el-button>
        </el-descriptions-item>
      </el-descriptions>
    </div>
    <el-empty v-else description="暫無數據"></el-empty>
    <el-row :gutter="10" justify="end" type="flex" class="text-right">
      <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="1"><el-button type="primary" @click="drawer = true">添加網絡硬盤</el-button></el-col>
    </el-row>
    <el-drawer
      title="添加網絡硬盤"
      :visible.sync="drawer"
      direction="btt"
      :wrapperClosable="false"
      ref="drawer"
      >
      <el-row :gutter="10" justify="end" type="flex" class="text-right">
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入網盤名稱" v-model="form.name">
            <template slot="prepend">名称</template>
          </el-input>
        </el-col>
        <el-col :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
          <el-input placeholder="請輸入網盤IP" v-model="form.dishLocation">
            <template slot="prepend">IP</template>
          </el-input>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="6" :xl="6">
          <el-button type="primary" @click="onAddAssets">確定</el-button>
          </el-col>
      </el-row>
    </el-drawer>
  </div>

</template>

<script>
import { timestampToTime } from '@/utils/tools'

export default {
  name: 'netdisk-index',

  data() {
    return {
      netdiskList: [],
      ruleForm: {
        userId: ''
      },
      loading: false,
      drawer: false,
      form: {
        name: '',
        dishLocation: ''
      }
    }
  },

  created() {
    let user = sessionStorage.getItem('user')
    if (user) {
      user = JSON.parse(user)
      this.ruleForm.userId = user.id || ''
    }
  },

  mounted() {
    this.getData()
  },

  methods: {
    //移除网络硬盘
    onDelFacility(id) {
      this.$confirm('此操作將移除id為' + id + '網絡硬盤, 是否繼續?', '提示', {
        confirmButtonText: '確定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$store.dispatch('netdisk/fetchDelDish', { dishId: id }).then(res => {
          // console.log('移除网络硬盘')
          // console.log(res)
          this.loading = false
          this.$message.success(res.message)
          this.getData()
        }).catch(err => {
          this.loading = false
          this.$message.error(err)
          return false
        })
      }).catch(() => {
        //取消动作
      })
    },

    //获取网络硬盘列表
    getData() {
      this.loading = true
      this.$store.dispatch('netdisk/fetchListUserDish').then(res => {
        // console.log('获取网络硬盘列表')
        // console.log(res)
        this.netdiskList = res
        if(this.netdiskList.length) {
          this.netdiskList.forEach(function(item) {
            item.time = window.Tools.timestampToTime(item.createTime)
          })
        }
        this.loading = false
        // this.$message.success(res.message)
      }).catch(err => {
        this.loading = false
        this.$message.error(err)
        return false
      })
    },

    //添加网络硬盘-确认按钮
    onAddAssets() {
      this.loading = true
      this.$store.dispatch('netdisk/fetchAddDish', this.form).then(res => {
        // console.log('添加网络硬盘')
        // console.log(res)
        this.loading = false
        this.$message.success(res.message)
        //关闭抽屉
        this.$refs.drawer.closeDrawer()
        this.form = {
          name: '',
          dishLocation: ''
        }
        this.getData()
      }).catch(err => {
        this.loading = false
        this.$message.error(err)
        return false
      })
    },
  }
}
</script>

<style scoped lang="scss">
@import "../../assets/css/them.scss";

.container-box {
  padding: 10px 20px;
}

/deep/ .el-descriptions .is-bordered {
  table-layout: fixed !important;
}

.green {
  color: $connectColor;
}

.red {
  color: $disconnectColor;
}

.text-right {
  padding: 20px;
  text-align: right;
}

/deep/.el-descriptions {
  margin-bottom: 10px !important;

  .el-descriptions-item__cell {
    word-break: break-all;
  }
}
</style>
